import { getIncomeListAxios, getTodayIncomeAxios, getWeekIncomeAxios, getMonthIncomeAxios, setStateAxios, getSearchDataAxios } from "../../http/apis/moneyApi/index"
import { timeFormat } from "../../utils/utils.js"

// 修改 tableList 的格式
function setTableList(tableList) {
  console.log("数据转换：", tableList);
  if(!tableList.financeTwo){
    tableList.financeTwo = []
  }
  let arr = [...tableList.financeOne,...tableList.financeTwo];
  let list = [];
  arr.forEach(item => {
        let obj = {
          money_id: item.finance_no,  // 编号，主键
          custome_id: item.finance_customer,    // 客户编号
          check: item.finance_state == 0 ? "待审核" : "已审核",   // 审核
          customername: item.customer_Name,   // 客户名称
          contact: item.finance_order_no,   // 关联
          contactperson: item.contact_name,    // 联系人
          inoutcome: item.rae_type_name,    // 收支支出
          inoutcategory: item.rae_category_name,   // 收支类别
          inoutmoney: item.finance_money,   // 收支金额
          remarks: item.finance_remarks,    // 备注
          inouttime: timeFormat(item.finance_date),     // 收支日期
          salesman: item.employee_name,    // 业务员
          inputtime: timeFormat(item.finance_date),    // 录入时间
          accessory: item.finance_enclosure == "-" ? "无附件" : "有附件",    // 附件
        };
        // 将数据加入到页面显示数据中
        list.push(obj);
  });
  return list;
}

export default {
  // 通过后端接口拿到收支列表的数据
  getIncomeListData(context, parmas) {
    return new Promise((resolve, reject) => {
      getIncomeListAxios(parmas)
        .then(res => {
          if (res.data.code === 200) {
            console.log("parmas:", parmas);
            console.log("res.data.data: ", res.data);
            let list = setTableList(res.data.data)
            context.commit('setTableListMutation', list)
            context.commit('setTotalDataMutation', res.data.count)
            resolve(res.data.count)
          } else {
            reject({
              code: res.data.code,
              msg: res.data.msg
            })
          }
        })
        .catch(err => {
          reject(err)
        })
    })
  },
  // 今日收支
  getTodayIncomeData(context, parmas) {
    return new Promise((resolve, reject) => {
      getTodayIncomeAxios(parmas)
        .then(res => {
          if (res.data.code === 200) {
            let list = setTableList(res.data.data)
            context.commit('setTableListMutation', list)
            context.commit('setTotalDataMutation', res.data.count)
            resolve(res.data.count)
          } else {
            reject({
              code: res.data.code,
              msg: res.data.msg
            })
          }
        })
        .catch(err => {
          reject(err)
        })

    })
  },
  // 本周收支
  getWeekIncomeData(context, parmas) {
    return new Promise((resolve, reject) => {
      getWeekIncomeAxios(parmas)
        .then(res => {
          if (res.data.code === 200) {
            let list = setTableList(res.data.data)
            context.commit('setTableListMutation', list)
            context.commit('setTotalDataMutation', res.data.count)
            resolve(res.data.count)
          } else {
            reject({
              code: res.data.code,
              msg: res.data.msg
            })
          }
        })
        .catch(err => {
          reject(err)
        })

    })
  },
  // 本月收支
  getMonthIncomeData(context, parmas) {
    return new Promise((resolve, reject) => {
      getMonthIncomeAxios(parmas)
        .then(res => {
          if (res.data.code === 200) {
            let list = setTableList(res.data.data)
            context.commit('setTableListMutation', list)
            context.commit('setTotalDataMutation', res.data.count)
            resolve(res.data.count)
          } else {
            reject({
              code: res.data.code,
              msg: res.data.msg
            })
          }
        })
        .catch(err => {
          reject(err)
        })
    })
  },
  // 设置状态审核删除
  getStateData(context, params, id) {
    return new Promise((resolve, reject) => {
      setStateAxios(params)
        .then(res => {
          if (res.data.code === 200) {
            if (params.finance_State == 1) {
              context.commit('setisStateMutation', id)
            } else if (params.finance_State == 0) {
              context.commit('setStatedMutation', id)
            }
            resolve()
          } else {
            reject({
              code: res.data.code,
              msg: res.data.msg
            })
          }
        })
        .catch(err => {
          reject(err)
        })
    })
  },

  // 设置普通搜索接口
  getSearchData(context, params) {
    return new Promise((resolve, reject) => {
      getSearchDataAxios(params)
        .then(res => {
          if (res.data.code === 200) {
            console.log("res.data:", res.data.count);
            let list = setTableList(res.data.data)
            context.commit('setTableListMutation', list)
            context.commit('setTotalDataMutation', res.data.count)
            resolve(res.data.count)
          }else if(res.data.code === 500) {
            context.commit('setTableListMutation', [])
          } else {
            reject({
              code: res.data.code,
              msg: res.data.msg
            })
          }
        })
        .catch(err => {
          reject(err)
        })
    })
  },
}